package javaweb.web;

import javaweb.pojo.User;
import javaweb.servlet.impl.BaseServlet;
import javaweb.servlet.impl.UserServletImpl;
import javaweb.utility.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

import static com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY;

/**
 * @author shkstart
 * @creat ${YEAR-}-07-24 22:26
 */

public class UserServlet extends BaseServlet {

    UserServletImpl usl = new UserServletImpl();
  public void Login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      User user1 = WebUtils.copyParamToBean(request.getParameterMap(), new User());
      User user = usl.logUser(user1);
     // System.out.println(user);
      if(user!=null){
          request.getSession().setAttribute("user",user);
           // request.setAttribute("user",user);
            //System.out.println("登录hen成功");
            request.getRequestDispatcher("/pages/user/login_success.jsp").forward(request,response);
        } else {
            request.setAttribute("msg","用户名或密码错误");

            request.setAttribute("username",request.getParameter("username"));
//            request.getRequestDispatcher("/pages/user/login_success.jsp").forward(request,response);
      request.getRequestDispatcher("/pages/user/login.jsp").forward(request,response);
      }

    }
    public void Regist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String token = (String)request.getSession().getAttribute(KAPTCHA_SESSION_KEY);
        //  1、获取请求的参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String email = request.getParameter("email");
        String code = request.getParameter("code");
//        2、检查 验证码是否正确  === 写死,要求验证码为:abcde
        if (token.equalsIgnoreCase(code)&&token!=null) {
//        3、检查 用户名是否可用
            if (usl.checkUserName(username)) {
                // System.out.println("用户名[" + username + "]已存在!");
                // 把回显信息，保存到requstuest域中
                request.setAttribute("msg", "用户名已存在！！");
                request.setAttribute("username", username);
                request.setAttribute("email", email);
//        跳回注册页面
                request.getRequestDispatcher("/pages/user/regist.jsp").forward(request, response);
            } else {
                //      可用
//                调用Sservice保存到数据库
                usl.registUser(new User( username, password, email));
//
//        跳到注册成功页面 regist_success_menu.jsp
                request.getRequestDispatcher("/pages/user/regist_success_menu.jsp").forward(request, response);
            }
        } else {
            // 把回显信息，保存到requstuest域中
            request.setAttribute("msg", "验证码错误！！");
            request.setAttribute("username", username);
            request.setAttribute("email", email);
            System.out.println("验证码[" + code + "]错误");
            request.getRequestDispatcher("/pages/user/regist.jsp").forward(request, response);
        }
    }
    protected void logOut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    request.getSession().invalidate();
    response.sendRedirect(request.getContextPath()+"/index.jsp");
    }
}
